package io.lkt.modules.sys.controller;

import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BPosterDetailsEntity;
import io.lkt.modules.sys.entity.BPosterInfoEntity;
import io.lkt.modules.sys.service.BPosterDetailsService;
import io.lkt.modules.sys.service.BPosterInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 海报信息内容详情表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-12-01 16:25:18
 */
@RestController
@RequestMapping("/sys/bposterdetails")
public class BPosterDetailsController {
    @Autowired
    private BPosterInfoService bPosterInfoService;

    @Autowired
    private BPosterDetailsService bPosterDetailsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bposterdetails:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bPosterDetailsService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/queryAll")
//    @RequiresPermissions("sys:bposterdetails:queryAll")
    public R queryAll(@RequestBody BPosterDetailsEntity bPosterDetailsEntity) {
        List<BPosterDetailsEntity> bPosterDetails = bPosterDetailsService.queryAll(bPosterDetailsEntity);
        return R.ok().put("bPosterDetails", bPosterDetails);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bposterdetails:info")
    public R info(@PathVariable("id") Integer id) {
        BPosterDetailsEntity bPosterDetails = bPosterDetailsService.getById(id);

        return R.ok().put("bPosterDetails", bPosterDetails);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bposterdetails:save")
    public R save(@RequestBody BPosterDetailsEntity bPosterDetails) {

        bPosterDetails.setCreateTime(new Date());
        bPosterDetails.setUpdateTime(new Date());
        bPosterDetailsService.save(bPosterDetails);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bposterdetails:update")
    public R update(@RequestBody BPosterDetailsEntity bPosterDetails) {
        ValidatorUtils.validateEntity(bPosterDetails);

        bPosterDetails.setUpdateTime(new Date());
        bPosterDetailsService.updateById(bPosterDetails);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bposterdetails:delete")
    public R delete(@RequestBody Integer[] ids) {
        bPosterDetailsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/saveEle")
//    @RequiresPermissions("sys:bposterdetails:saveEle")
    public R saveEle(@RequestBody List<BPosterDetailsEntity> bPosterDetails) {
        ValidatorUtils.validateEntity(bPosterDetails);

        if (CollectionUtils.isNotEmpty(bPosterDetails)) {
            // 查询海报详情为正常状态
            BPosterDetailsEntity delInfo = new BPosterDetailsEntity();
            delInfo.setType(bPosterDetails.get(0).getType());
            delInfo.setStatus(1);
            List<BPosterDetailsEntity> bPosterDetailsList = bPosterDetailsService.queryAll(delInfo);

            // 更新海报详情为删除状态
            if (CollectionUtils.isNotEmpty(bPosterDetailsList)) {
                for (BPosterDetailsEntity info : bPosterDetailsList) {
                    info.setStatus(0);
                    info.setUpdateTime(new Date());
                    bPosterDetailsService.updateById(info);
                }
            }

            // 更新海报信息表底图
            for (BPosterDetailsEntity info : bPosterDetails) {
                if (info.getItem() == 1011 && StringUtils.isNotBlank(info.getImange())) {
                    // 更新海报底图
                    Integer posterInfoId = info.getType();
                    BPosterInfoEntity bPosterInfo = bPosterInfoService.getById(posterInfoId);
                    bPosterInfo.setBase(info.getImange());
                    bPosterInfo.setUpdateTime(new Date());
                    bPosterInfoService.updateById(bPosterInfo);
                }
            }

            // 保存海报详情表元素
            for (BPosterDetailsEntity info : bPosterDetails) {
                info.setStatus(1);
                info.setCreateTime(new Date());
                info.setUpdateTime(new Date());
                bPosterDetailsService.save(info);
            }

            return R.ok();
        }

        return R.error();
    }

}
